<?php
class main extends spController{
	function index(){
		echo '欢迎使用ICase Acl控制台</br>';
		echo 'By Pony cuis@vip.qq.com</br>';
		
		echo '正在检测ICase2类是否存在于SpeedPHP中：';
		spClass('libICase');
		echo '检测正常.............OK</br>';
		
		echo '正在检测spICase模块是否存在于SpeedPHP中：';
		spClass('spICase');
		echo '检测正常.............OK</br>';
		
		echo '正在检测ICase是否已经安装：';
		$a = spAccess('r','ICASE_REGISTER_GROUP');
		$b = spAccess('r','ICASE_PREFIX');
		if(!empty($a) || !empty($b)){
			echo '已经安装，以下是配置选项</br>';
			$this -> config();
		}
		else{
			echo 'ICase尚未安装，以下是安装选项</br>';
			$this -> install();
		}
	}
	
	function install(){
		echo '<form action='.spUrl('main','toinstall').' method=post>';
		echo 'ICase数据表前缀：<input type=text name=PREFIX value=sp /></br>';
		echo 'ICase默认注册用户组：<input type=text name=REGISTER_GROUP value=2 readonly >';
		echo '（请在安装完成后修改）</br>';
		echo '<input type=submit name=submit value=submit>';
		echo '</form>';
	}
	
	function toinstall(){
		$mysql = new mod_install();
		$run = $mysql -> runSql('CREATE TABLE '.$this -> spArgs('PREFIX').'_acl_groups (
  id int(8) NOT NULL auto_increment,
  `name` varchar(200) collate utf8_bin NOT NULL,
  `type` varchar(3) collate utf8_bin NOT NULL default "min",
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ;');
		$run = $mysql -> runSql("INSERT INTO {$this -> spArgs('PREFIX')}_acl_groups (id, name, type) VALUES (1, '管理员', 'max');");
		$run = $mysql -> runSql("INSERT INTO {$this -> spArgs('PREFIX')}_acl_groups (id, name, type) VALUES (2, '用户', 'min');");
		$run = $mysql -> runSql("INSERT INTO {$this -> spArgs('PREFIX')}_acl_groups (id, name, type) VALUES (-1, '游客', 'min');");
		$run = $mysql -> runSql("CREATE TABLE {$this -> spArgs('PREFIX')}_acl_rules (
  id int(8) NOT NULL auto_increment,
  groups_id int(8) NOT NULL,
  controller varchar(100) collate utf8_bin NOT NULL,
  `action` varchar(100) collate utf8_bin NOT NULL,
  `code` varchar(100) collate utf8_bin NOT NULL,
  allow tinyint(1) NOT NULL,
  `start` int(10) NOT NULL,
  `end` int(10) NOT NULL,
  note text collate utf8_bin NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;");
		$run = $mysql -> runSql("INSERT INTO {$this -> spArgs('PREFIX')}_acl_rules (id, groups_id, controller, action, code, allow, start, end, note) VALUES (1, -1, 'main', 'index', '', 1, 0, 0, '');");
		$run = $mysql -> runSql("CREATE TABLE {$this -> spArgs('PREFIX')}_acl_users (
  id int(8) NOT NULL auto_increment,
  username varchar(60) collate utf8_bin NOT NULL,
  `password` varchar(32) collate utf8_bin NOT NULL,
  salt varchar(16) collate utf8_bin NOT NULL,
  email varchar(100) collate utf8_bin NOT NULL,
  groups_id int(8) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY username (username),
  UNIQUE KEY email (email)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;");
		$run = $mysql -> runSql("CREATE TABLE `{$this -> spArgs('PREFIX')}_acl_connects` (
  `id` int(8) NOT NULL auto_increment,
  `website` varchar(50) NOT NULL,
  `webuser` varchar(200) NOT NULL,
  `users_id` int(8) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `website` (`website`,`webuser`,`users_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;");
		if($run){
			spAccess('w','ICASE_REGISTER_GROUP',$this -> spArgs('REGISTER_GROUP'));
			spAccess('w','ICASE_PREFIX',$this -> spArgs('PREFIX'));
			$this -> jump(spUrl('main','index'));
		}
		else{
			$this -> error('安装失败，请检查SPEEDPHP数据库配置是否正确',spUrl('main','index'));
		}
	}
	
	function config(){
		echo '<a href="'.spUrl('main','edit_register_group').'">修改新注册用户归属组</a>';
	}
	
	function edit_register_group(){
		if($this -> spArgs('submit')){
			spAccess('w','ICASE_REGISTER_GROUP',$this -> spArgs('groups_id'));
			$this -> success('成功将组ID'.$this -> spArgs('groups_id').'设为默认注册组',spUrl('main','index'));
		}
		$groups = spClass('libICase') -> acl_group_get();
		for($i=0;isset($groups[$i]);$i++){
			echo $groups[$i]['id'].'组名称：'.$groups[$i]['name'].'(<a href="'.spUrl('main','edit_register_group',array('groups_id'=>$groups[$i]['id'],'submit'=>true)).'">选定此组</a>)</br>';
		}
	}
}

class mod_install extends spModel{
	
}